/**
 * RuleFactor 规则因子对象
 */

import RuleFactorValue, { MATCHING_TYPE } from './RuleFactorValue'

// const MATCHING_TYPE = {
//   equals: 'equals',
//   lessThan: 'lessThan',
//   greaterThan: 'greatgerThan',
//   inRange: 'inRange'
// }

// const matchingTypeLabel = {
//   [MATCHING_TYPE.equals]: '相等',
//   [MATCHING_TYPE.lessThan]: '小于',
//   [MATCHING_TYPE.greaterThan]: '大于',
//   [MATCHING_TYPE.inRange]: '满足区间'
// }

const FACTOR_DATA_TYPE = {
  String: 'String',
  Integer: 'Integer',
  Double: 'Double',
  BigDecimal: 'BigDecimal',
  Date: 'Date'
}

const MATCHING_TYPES_BY_DATA_TYPE = {
  [FACTOR_DATA_TYPE.String]: [
    MATCHING_TYPE.equal,
    MATCHING_TYPE.notEqual,
    MATCHING_TYPE.startWith,
    MATCHING_TYPE.endWith,
    MATCHING_TYPE.strContains,
    MATCHING_TYPE.isNotNull,
    MATCHING_TYPE.isNull,
    MATCHING_TYPE.bizJoinCFG,
    MATCHING_TYPE.bizInCfg,
    MATCHING_TYPE.cfgInBiz,
    MATCHING_TYPE.containAndDomain
  ],
  [FACTOR_DATA_TYPE.Integer]: [
    MATCHING_TYPE.equal,
    MATCHING_TYPE.notEqual,
    MATCHING_TYPE.domain,
    MATCHING_TYPE.isNotNull,
    MATCHING_TYPE.isNull,
    MATCHING_TYPE.bizJoinCFG,
    MATCHING_TYPE.bizInCfg,
    MATCHING_TYPE.cfgInBiz
  ],
  [FACTOR_DATA_TYPE.Double]: [
    MATCHING_TYPE.equal,
    MATCHING_TYPE.notEqual,
    MATCHING_TYPE.domain,
    MATCHING_TYPE.isNotNull,
    MATCHING_TYPE.isNull,
    MATCHING_TYPE.bizJoinCFG,
    MATCHING_TYPE.bizInCfg,
    MATCHING_TYPE.cfgInBiz
  ],
  [FACTOR_DATA_TYPE.BigDecimal]: [
    MATCHING_TYPE.equal,
    MATCHING_TYPE.notEqual,
    MATCHING_TYPE.domain,
    MATCHING_TYPE.isNotNull,
    MATCHING_TYPE.isNull,
    MATCHING_TYPE.bizJoinCFG,
    MATCHING_TYPE.bizInCfg,
    MATCHING_TYPE.cfgInBiz
  ],
  [FACTOR_DATA_TYPE.Date]: [
    MATCHING_TYPE.equal,
    MATCHING_TYPE.notEqual,
    MATCHING_TYPE.domain,
    MATCHING_TYPE.dateDomain,
    MATCHING_TYPE.isNotNull,
    MATCHING_TYPE.isNull,
    MATCHING_TYPE.bizJoinCFG,
    MATCHING_TYPE.bizInCfg,
    MATCHING_TYPE.cfgInBiz
  ]
}

class RuleFactor {
  // 因子代码
  code

  // 因子标签，用于显示
  label

  // 因子数据类型，枚举值
  dataType

  // 因子枚举代码
  enumCode

  // 因子描述信息，用于说明因子用途
  desc

  // 因子是否可在规则表中编辑
  isEditableInRuleTable

  // 因子默认匹配规则
  // matchingRule

  // 因子值列表，每个 element 都是 RuleFactorValue 对象
  values = []

  constructor ({
    code,
    label,
    desc,
    dataType = FACTOR_DATA_TYPE.String,
    enumCode,
    isEditableInRuleTable = true,
    values = []
  } = {}) {
    this.code = code
    this.label = label
    this.desc = desc
    this.isEditableInRuleTable = isEditableInRuleTable
    // this.matchingRule = matchingRule
    this.dataType = dataType
    this.enumCode = enumCode
    this.values = this.initValues(values)
  }

  initValues (values = []) {
    const valueList = []
    values.forEach((value) => {
      valueList.push(this.buildValue(value))
    })
    return valueList
  }

  buildValue (value) {
    if (value instanceof RuleFactorValue) {
      return value
    }
    return new RuleFactorValue(value)
  }

  // 添加因子，默认 push
  addValue (value) {
    this.values.push(this.buildValue(value))
  }

  // 删除因子
  deleteValue (value) {
    this.values.splice(this.findValueIndex(value), 1)
  }

  // 查询因子 index
  findValueIndex (value) {
    return this.values.findIndex((item) => item.code === value.code)
  }

  // 获取因子值
  getValueWithCode (valueCode) {
    return this.values.find((value) => value.code === valueCode)
  }
}

export default RuleFactor
export { FACTOR_DATA_TYPE, MATCHING_TYPES_BY_DATA_TYPE }
